<template>
  <t-dialog :visible.sync="visible" :header="header" confirmBtn="提交" :confirmLoading="confirmLoading"
    :onConfirm="submit" :onClose="closeDialog" :closeOnOverlayClick="false" placement="center" width="60%">
    <div style="height: 40vh; width: 98%" v-if="!dataLoading">
      <t-form ref="form" :data="formData" :rules="rules" :colon="true" labelWidth="140px" labelAlign="right">
        <t-row :gutter="[14, 25]">
          <t-col :span="6">
            <t-form-item label="人才信息" name="talentsId">
              <t-select clearable v-model="formData.talentsId">
                <t-option v-for="item in pidData" :value="item.id" :label="item.name" :key="item.id"></t-option>

              </t-select>
            </t-form-item>
            <t-form-item label="项目名称" name="projectName">
              <t-input clearable v-model="formData.projectName"></t-input>
            </t-form-item>
            <t-form-item label="项目类别" name="projectType">
              <t-select clearable v-model="formData.projectType" :options="dict.type.project_type">
              </t-select>
            </t-form-item>
            <t-form-item label="学科分类" name="subjectClassification">
              <t-select placeholder="学科分类" clearable :options="dict.type.subject_classification"
                v-model="formData.subjectClassification">
              </t-select>
            </t-form-item>
            <t-form-item label="项目金额（万元）" name="adviceFunding">
              <t-input clearable v-model="formData.adviceFunding"></t-input>
            </t-form-item>


          </t-col>
          <t-col :span="6">
            <t-form-item label="关键词" name="keyWord">
              <t-input clearable v-model="formData.keyWord"></t-input>
            </t-form-item>
            <t-form-item label="项目开始日期" name="startDate">
              <t-date-picker filterable mode="date" clearable v-model="formData.startDate"></t-date-picker>
            </t-form-item>
            <t-form-item label="项目结束日期" name="endDate">
              <t-date-picker filterable mode="date" clearable v-model="formData.endDate"></t-date-picker>
            </t-form-item>

            <t-form-item label="补助经费（万元）" name="subsidyFunding">
              <t-input clearable v-model="formData.subsidyFunding"></t-input>
            </t-form-item>
            <t-form-item label="上传附件" name="kjrcrcxm">
              <file-upload v-model="formData.sysAttMainVo.sysAttMainIds" moduleKey="kjrcrcxm" :fileList="fileLists"
                :multiple="true">
                <t-button variant="outline">
                  <t-icon name="cloud-upload" slot="icon" />
                  点击上传
                </t-button>
              </file-upload>
            </t-form-item>
          </t-col>
        </t-row>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue"
import { getFiles } from "@/api/common";

export default {
  name: "unit-add-edit",
  dicts: ['kpzh_manage_org_code', 'kpzh_national_economy_code', 'kpzh_unit_level', 'kpzh_source_funds', "project_type", "subject_classification"],
  components: {
    FileUpload
  },
  props: {
    objId: {
      type: [String, Number],
      default: '',
    },
    header: {
      type: String,
      default: '新增'
    },
    row: {
      type: Object,
      default: () => ({})
    },
    isUpdate: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler (val) {
        if (val) {

        }
      }
    },
  },
  data () {
    return {
      visible: true,
      formData: {
        talentsId: '',
        projectName: '',
        projectType: '',
        subjectClassification: '',
        keyWord: '',
        startDate: '',
        endDate: '',
        adviceFunding: '',
        subsidyFunding: '',
        sysAttMainVo: {
          sysAttMainIds: []
        }
      },
      pidData: [],
      fileIds: [],
      fileLists: [],
      dataLoading: false,
      locationOptions: [],
      menuOptions: [],
      confirmLoading: false,
      rules: {
        talentsId: [
          { required: true, message: "人才信息不能为空" }
        ],
        projectName: [
          { required: true, message: "项目名称不能为空" }
        ],
        projectType: [
          { required: true, message: "项目类别不能为空" }
        ],
        subjectClassification: [
          { required: true, message: "学科分类不能为空" }
        ],
        keyWord: [
          { required: true, message: "关键词不能为空" }
        ],
        startDate: [
          { required: true, message: "项目开始日期不能为空" }
        ],
        endDate: [
          { required: true, message: "项目结束日期不能为空" }
        ],
        adviceFunding: [
          { required: true, message: "项目金额不能为空" }
        ],
        subsidyFunding: [
          { required: true, message: "补助经费不能为空" }
        ],
      },
      queryParams: {
        pageNum: 1,
        pageSize: 100000,
        name: "",
        status: "",
        dateRange: []
      },
      treeProps: {
        keys: {
          value: 'id'
        },
      }
    }
  },
  created () {
    if (this.isUpdate) {
      this.getObj();
    }

    Object.assign(this.formData, this.row)
    // this.getTreeSelect();
    // this.getLocation();
    this.getList()
  },
  mounted () {

    this.$nextTick(() => {
      this.$refs.form.reset()
    })
  },
  methods: {
    getObj () {
      this.$api.kjrc.talentsProject.getDetailData(this.objId).then(response => {
        response.data.sysAttMainVo = response.data.sysAttMainVo || { sysAttMainIds: [] }
        Object.assign(this.formData, response.data)
        getFiles(this.objId).then((res) => {
          this.fileLists = []
          const ids = [];
          if (res.data && res.data.length) {
            res.data.forEach((item) => {
              ids.push(item.id);
              this.fileLists.push({ name: item.fileName, url: item.fullFilePath, id: item.id, filePath: item.filePath })
            })
            this.formData.sysAttMainVo.sysAttMainIds = ids;
          }
        });
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getList () {
      this.$api.kjrc.talents.myList({ pageNum: 1, pageSize: 10000 }).then(response => {
        this.pidData = response.rows;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog () {
      this.$emit("update:visible", false);
    },
    treeChange (value, context) {
      this.formData.orgStats = context.data?.id
    },
    treeClear () {
      this.formData.orgStats = null
    },
    submit () {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {

          this.confirmLoading = true

          if (this.isUpdate) {
            this.$api.kjrc.talentsProject.update({
              ...this.formData,
              // sysAttMainVo: { sysAttMainIds: this.formData.fileIds }
            }).then(res => {
              this.confirmLoading = false
              this.visible = false
              this.$emit("update:visible", false);
              this.$emit("reload")
            }).catch((e) => {
              this.$message.error(e.toString());
              this.confirmLoading = false
            });
          } else {
            this.$api.kjrc.talentsProject.add({
              ...this.formData,
              // sysAttMainVo: { sysAttMainIds: this.formData.fileIds }
            }).then(res => {
              this.confirmLoading = false
              this.visible = false
              this.$emit("update:visible", false);
              this.$emit("reload")
            }).catch((e) => {
              this.$message.error(e.toString());
              this.confirmLoading = false
            });
          }

        }
      })
    },
    getTreeSelect () {
      this.$api.kpzh.unit.treeSelect().then(response => {
        this.menuOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getLocation () {
      return this.$api.system.location.treeselect().then(response => {
        this.locationOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
  },
}

</script>

<style scoped></style>
